<template>
  <div class="main-view" v-loading="state.loading">
    <div class="main-view-info" v-if="state.listData.length">
      <div class="table-wrapper">
        <el-table style="height: 100%" :data="state.listData" border>
          <el-table-column
            label="序号"
            type="index"
            width="60"
            align="center"
          ></el-table-column>
          <el-table-column
            label="名称"
            show-overflow-tooltip
            align="center"
            prop="doc_type"
            width="200"
          >
          </el-table-column>
          <el-table-column
            label="电话号码"
            show-overflow-tooltip
            prop="mobile"
            align="center"
            width="200"
          >
          </el-table-column>
          <el-table-column
            label="文书文件"
            show-overflow-tooltip
            align="center"
          >
            <template #default="scope">
              <div>
                <img
                  style="width: 40px"
                  src="@/assets/img/icon-file.png"
                  alt=""
                />
              </div>
              <div class="file-name" style="color: #000; font-size: 16px">
                {{ scope.row.source }}
              </div>
              <el-button
                size="small"
                type="primary"
                class="btn-download"
                @click="downloadFile(scope.row.id, scope.row.source)"
              >
                下载文件
              </el-button>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center">
            <template #default="scope">
              <div v-if="!scope.row.check">
                <el-upload
                  :action="
                    serverUrl + '/admin/doc/upload-check?doc_id=' + scope.row.id
                  "
                  :on-success="getDocsCheckList"
                  :on-change="handleProgress"
                  :before-upload="handleBeforeUpload"
                  :show-file-list="false"
                  :headers="{
                    'token-admin': state.token,
                  }"
                  v-loading="state.uploading"
                >
                  <div class="el-upload__tip">请上传PDF文件</div>
                  <div>
                    <el-button size="small" type="danger" class="btn-upload"
                      >点击上传</el-button
                    >
                  </div>
                </el-upload>
              </div>
              <div v-else>
                <div class="file-name" style="color: #999999; font-size: 14px">
                  {{ scope.row.check }}
                </div>
                <div>
                  <el-button size="small" type="success" class="btn-success">
                    已上传
                  </el-button>
                </div>
                <div>
                  <el-upload
                    :action="
                      serverUrl +
                      '/admin/doc/upload-check?doc_id=' +
                      scope.row.id
                    "
                    :on-success="getDocsCheckList"
                    :before-upload="handleBeforeUpload"
                    :show-file-list="false"
                    :headers="{
                      'token-admin': state.token,
                    }"
                    :on-change="handleProgress"
                  >
                    <el-button size="small" class="btn-edit"> 修改 </el-button>
                  </el-upload>
                </div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="right-view-page">
        <el-pagination
          layout="total, prev, pager, next"
          :total="state.total"
          @current-change="handleCurrentChange"
          :page-size="10"
          :pageSizes="[10, 20, 30, 40]"
          :current-page="state.currentPage"
        >
        </el-pagination>
      </div>
    </div>
    <div v-else class="right-view-nodata">
      <img src="@/assets/img/no-data.png" alt="" />
      <div>暂无数据</div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, watch } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import API from "@/utils/api";
import { serverUrl } from "@/utils/const";
const state = reactive({
  loading: false,
  uploading: false,
  listData: [],
  total: 0,
  currentPage: 1,
  token: localStorage.getItem("token"),
});
onMounted(() => {
  getDocsCheckList();
});
// 获取文书列表
const getDocsCheckList = () => {
  state.loading = true;
  let params = {
    page: state.currentPage,
    limit: 10,
  };
  API.docsCheck.getDocsCheckListApi(params, (res) => {
    state.loading = false;
    if (res.code == 0) {
      state.listData = res.data.details;
      state.total = res.data.total;
    }
  });
};
// 上传前校检格式和大小
const handleBeforeUpload = (file) => {
  console.log(file.type);
  const type = ["application/pdf"];
  const isPDF = type.includes(file.type);
  // 检验文件格式
  if (!isPDF) {
    ElMessage.error(`只能上传PDF文件!`);
    state.loading = false;
    return false;
  }
  // 校检文件大小
  const isLt = file.size / 1024 / 1024;
  if (isLt > 5) {
    ElMessage.error(`上传文件大小不能超过 5 MB!`);
    state.loading = false;
    return false;
  }

  return true;
};
const handleProgress = (file, filelist) => {
  if (file.percentage < 100) {
    state.loading = true;
  } else {
    state.loading = false;
  }
};
// 下载文书
const downloadFile = (id, name) => {
  let params = {
    doc_id: id,
  };
  state.loading = true;
  API.docsCheck.downloadFileApi(
    params,
    (result) => {
      if (result) {
        const url = window.URL.createObjectURL(new Blob([result]));
        const link = document.createElement("a");
        link.href = url;
        link.setAttribute("download", name);
        document.body.appendChild(link);
        link.click();
      }
      state.loading = false;
    },
    () => {
      state.loading = false;
    }
  );
};

const handleCurrentChange = (val) => {
  state.currentPage = val;
  getDocsCheckList();
};
</script>

<style lang="scss" scoped>
.main-view {
  padding: 20px;
  height: 100%;
  overflow: hidden;
  box-sizing: border-box;
  .main-view-info {
    display: flex;
    flex-direction: column;
    overflow: hidden;
    height: 100%;
    .table-wrapper {
      flex: 1;
      overflow: auto;
    }
  }

  .right-view-nodata {
    margin-top: 160px;
  }

  .right-view-page {
    display: flex;
    justify-content: end;
    margin-top: 15px;
  }
  ::v-deep .el-upload {
    flex-direction: column;
    height: auto;
  }

  .btn-download {
    color: #419eff;
    background: #b1d6fd;
    border: 1px solid #419eff;
    width: 100px;
    height: 34px;
    margin-top: 14px;
  }
  .btn-upload {
    color: #ff4141;
    background: #fdb1b1;
    border: 1px solid #ff4141;
    width: 100px;
    height: 34px;
    margin-top: 24px;
  }
  .btn-success {
    color: #00cb3c;
    background: #bfffd2;
    border: 1px solid #00cb3c;
    width: 100px;
    height: 34px;
    margin-top: 24px;
  }
  .btn-edit {
    height: 34px;
    color: #999999;
    background: #e7e7e7;
    border: 1px solid #999999;
    width: 100px;
    margin-top: 10px;
  }

  .file-name {
    max-width: 80%;
    margin: 0 auto;
    word-break: break-all;
    word-wrap: break-word;
    white-space: normal;
  }
}
</style>
